package com.wangshb.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshb.upms.core.constants.NumConstant;
import com.wangshb.upms.core.exception.OptimisticLockException;
import com.wangshb.upms.core.tree.TreeNode;
import com.wangshb.upms.core.utils.AssertUtil;
import com.wangshb.upms.entity.UpmsRole;
import com.wangshb.upms.mapper.UpmsRoleMapper;
import com.wangshb.upms.service.UpmsRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import  com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UpmsRoleServiceImpl extends ServiceImpl<UpmsRoleMapper, UpmsRole> implements UpmsRoleService {

    @Resource
    private UpmsRoleMapper upmsRoleMapper;

    @Override
    public Page<UpmsRole> findPage(int pageNo, int pageSize, UpmsRole condition) {

        Page<UpmsRole> page = new Page<>(pageNo, pageSize, true);
        page.setRecords(findList(page, condition));
        return page;
    }

    @Override
    public List<UpmsRole> findList(Page<UpmsRole> page, UpmsRole condition) {
        return this.upmsRoleMapper.findList(page, condition);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addUpmsRole(UpmsRole upmsRole) {

        // 查看用户的登陆账号是否存在
        UpmsRole dbUpmsRole = this.findOne(UpmsRole.builder().roleCode(upmsRole.getRoleCode()).build());
        AssertUtil.isNull(dbUpmsRole, "角色编码已经存在！");

        this.upmsRoleMapper.insert(upmsRole);
        return upmsRole.getId();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delUpmsRole(UpmsRole upmsRole) {
        UpmsRole delEntity = new UpmsRole();
        delEntity.setId(upmsRole.getId());
        delEntity.setVersion(upmsRole.getVersion());
        delEntity.setDeleted(NumConstant.ONE);
        updateUpmsRoleWithLock(delEntity);
    }

    @Override
    public UpmsRole findOne(UpmsRole upmsRole) {
        QueryWrapper<UpmsRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(upmsRole);
        return this.upmsRoleMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUpmsRoleWithLock(UpmsRole upmsRole) {

        AssertUtil.notNull(upmsRole, "角色信息不能为空");
        AssertUtil.notNull(upmsRole.getVersion(), "版本号不能为空");

        // 如果角色编码不为空，需要校验角色编码是否已经存在
        if (StringUtils.isNotBlank(upmsRole.getRoleCode())) {
            UpmsRole dbUpmsRole = this.findOne(UpmsRole.builder().roleCode(upmsRole.getRoleCode()).build());
            AssertUtil.isTrue(null == dbUpmsRole || dbUpmsRole.getId().equals(upmsRole.getId()), "角色编码已经存在！");
        }

        if (!this.updateById(upmsRole)) {
            throw new OptimisticLockException("数据已被修改");
        }
    }

    @Override
    public List<TreeNode> findForRoleTree() {
        return this.upmsRoleMapper.findForRoleTree();
    }
}
